﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraTreeList.Nodes;

namespace APPLICATION.Dict.EBK
{
    public partial class frmEdit_EBKFBK : Form
    {
        public BindingSource bs;
        public DataTable dt;
        public string strSPName;
        public string strADDorEDIT;

        public frmEdit_EBKFBK()
        {
            InitializeComponent();
        }

        private void frmEdit_EBKFBK_Load(object sender, EventArgs e)
        {
            choiser1.ConnectionString = globals.APPCONNSTRING;
            
            this.treeList1.DataSource = dt;
            this.txtMgr.DataBindings.Add("Text", dt, "hName");
            this.txtMgr.Enabled = false;
            this.treeList1.ExpandAll();

            DataRowView dr = (DataRowView)bs.Current;

            if (strADDorEDIT == "edit")
            {
                btnChange.Visible = true;
                //btnChange.Location.X.
                this.txthName.DataBindings.Add("Text", bs, "hName");
                this.txthCode.DataBindings.Add("Text", bs, "hCode");
                //this.txthCode.Properties.MaxLength=
                //this.txthCode.tex


                if (dr["mgrid"] is DBNull)
                {
                    ckbNoReferrer.Checked = true;
                }
                else
                {
                    int intMgrID = (int)dr["mgrid"];
                    DevExpress.XtraTreeList.Nodes.TreeListNode trnode;
                    trnode = this.treeList1.FindNodeByFieldValue("CodeID", intMgrID);
                    this.treeList1.SetFocusedNode(trnode);
                }

            }
            else
            {
                int intMgrID = (int)dr["CodeID"];
                DevExpress.XtraTreeList.Nodes.TreeListNode trnode;
                trnode = this.treeList1.FindNodeByFieldValue("CodeID", intMgrID);
                this.treeList1.SetFocusedNode(trnode);

                btnSave.Visible = true;
            }
        }

        private void ckbNoReferrer_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbNoReferrer.Checked)
            {
                txtMgr.Text = "";
                navbarControl1.Enabled = false;
            }
            else
            {
                navbarControl1.Enabled = true;
                //navbarControl1
                //navbarControl1.State = DevExpress.XtraNavBar.NavBarState.ExpandButtonPressed;

            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection cnSQL=new SqlConnection(globals.APPCONNSTRING);
            System.Data.SqlClient.SqlCommand cmdSQL=new SqlCommand();
            cmdSQL.CommandText = this.strSPName;
            cmdSQL.CommandType = CommandType.StoredProcedure;
            cmdSQL.Connection = cnSQL;
            cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 0;
            cmdSQL.Parameters.Add("@hName", SqlDbType.NVarChar, 200).Value = txthName.Text;
            cmdSQL.Parameters.Add("@hCode", SqlDbType.VarChar, 2).Value = txthCode.Text;

            if (ckbNoReferrer.Checked==false)
            {
                cmdSQL.Parameters.Add("@mgrid", SqlDbType.Int).Value = Convert.ToInt32( (object) dt.Rows[this.BindingContext[dt].Position]["CodeID"]);
            }
            try
            {
                cnSQL.Open();
                cmdSQL.ExecuteNonQuery();
                cnSQL.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show("Ошибка сохранения данных! \n" + ex.Message, "TIMS");
            }
        }

        private void btnChange_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection cnSQL = new SqlConnection(globals.APPCONNSTRING);
            System.Data.SqlClient.SqlCommand cmdSQL = new SqlCommand();
            DataRowView drv=(DataRowView) bs.Current;
            int intCodeID = (int) drv["CodeID"];

            cmdSQL.CommandText = this.strSPName;
            cmdSQL.CommandType = CommandType.StoredProcedure;
            cmdSQL.Connection = cnSQL;
            cmdSQL.Parameters.Add("@Action", SqlDbType.TinyInt).Value = 1;
            cmdSQL.Parameters.Add("@hName", SqlDbType.NVarChar, 200).Value = txthName.Text;
            cmdSQL.Parameters.Add("@CodeID", SqlDbType.Int).Value = intCodeID;
            cmdSQL.Parameters.Add("@hCode", SqlDbType.VarChar, 2).Value = txthCode.Text;

            if (ckbNoReferrer.Checked == false)
            {
                cmdSQL.Parameters.Add("@mgrid", SqlDbType.Int).Value = Convert.ToInt32((object)dt.Rows[this.BindingContext[dt].Position]["CodeID"]);
            }
            try
            {
                cnSQL.Open();
                cmdSQL.ExecuteNonQuery();
                cnSQL.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Ошибка сохранения данных! \n" + ex.Message, "TIMS");
            }
        }

        private void panelControl1_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
